package com.jsy.common.vo;

import lombok.Data;

import java.io.Serializable;

@Data
public class Page implements Serializable {
    //upper page
    private Integer upperPageNum;
    //current page
    private Integer pageNum;
    //pageSize
    private Integer pageSize;
    //total hits
    private long total;
    //total pages
    private Integer totalPages;
    //offset on both sides
    private Integer offset = 2;
    //the first three points
    private Boolean frontDot;
    //the last three points
    private Boolean backDot;
    //next pageNum
    private Integer nextPageNum;
    //begin
    private Integer start;
    //end
    private Integer end;

    public Page(String pageNum, String pageSize, String total, String totalPages) {
        this.pageNum = Integer.parseInt(pageNum);
        this.pageSize = Integer.parseInt(pageSize);
        this.total = Integer.parseInt(total);
        this.totalPages = Integer.parseInt(totalPages);
        //initialize
        initPage();
    }

    /**
     * 初始化并计算各个属性
     */
    private void initPage() {
        //处理上下一页
        upperAndNextPageNum();
        //处理要显示的页号数
        handlePageNumList();
        //处理前后3个点
        handleDot();
    }

    /**
     * 处理前后三个点
     */
    private void handleDot() {
        this.frontDot = false;
        if (this.start > 1) {
            this.frontDot = true;
        }
        this.backDot = false;
        if (this.end < this.totalPages) {
            this.backDot = true;
        }
    }

    /**
     * 处理要显示的也好书及前后3个点
     */
    private void handlePageNumList() {
        this.start = 1;
        this.end = this.totalPages;

        //导航条中最多显示的页号数=2*偏移量+1
        int totalShowPageNum = 2 * this.offset + 1;
        //是否有足够可显示页，只有总页数大于要显示的页数时才有需要计算开始、结束的页号
        if (totalShowPageNum < this.totalPages) {
            this.start = this.pageNum - this.offset;
            this.end = this.pageNum + this.offset;
        }
        if (this.start > 0) {
            if (this.end > this.totalPages) {
                this.end = this.totalPages;
                this.start = this.end - totalShowPageNum + 1;
            }
        } else {
            this.start = 1;
            this.end = totalShowPageNum;
        }
    }

    /**
     * 处理上一页页号、下一页页号
     */
    private void upperAndNextPageNum() {
        this.upperPageNum = this.pageNum - 1;
        if (this.upperPageNum < 0) {
            this.upperPageNum = 0;
        }
        this.nextPageNum = this.pageNum + 1;
        if (this.nextPageNum > this.totalPages) {
            this.nextPageNum = this.totalPages;
        }
    }
}
